package com.sunwayworld.basemodule.common.eptstorage.file.excelread.service.impl;

import com.sunwayworld.basemodule.common.eptstorage.file.excelread.listener.ExcelRowData;
import com.sunwayworld.basemodule.common.eptstorage.file.excelread.listener.SheetData;
import com.sunwayworld.basemodule.common.eptstorage.file.excelread.service.IExcelDataStorageService;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther zhangs
 * @description
 * @date 2022/1/18
 */
@Service
@GikamBean
public class MongoExcelDataStorageService implements IExcelDataStorageService {

//    @Autowired
    public MongoTemplate mongoTemplate;

    @Override
    public List<Map<String, Object>> storage(List<SheetData> sheetDataList, Map<String, Object> businessMap) {
        List<Map<String, Object>> resultMaps = new ArrayList<>();
        /*for (SheetData sheetData : sheetDataList) {
            //存入mongo
            //存入数据库
            // 存储clickhouse
        }*/
        // 删除历史上传的数据
        for (SheetData sheetData : sheetDataList) {
            String document = sheetData.getMongoDocument();
            if (businessMap != null) {
                Object matrixId = businessMap.get("matrixId");
                if (matrixId != null) {
                    Long matrixId1 = (Long) matrixId;
                    Query mtxQuery =  new Query(Criteria.where("matrixId").is(matrixId1));
                    BulkOperations ops = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,document);
                    ops.remove(mtxQuery);
                    ops.execute();
                }
            }
        }
        // 插入新的原始数据
        for (SheetData sheetData : sheetDataList) {
            List<Map<String, Object>> maps = this.putToMongo(sheetData, sheetData.getMongoDocument(), businessMap);
            if (maps != null) {
                resultMaps.addAll(maps);
            }
        }

        return resultMaps;
    }


    public List<Map<String, Object>> putToMongo(SheetData sheetData, String document, Map<String, Object> businessMap) {
        List<ExcelRowData> rowDataList = sheetData.getRowDataList();
        if(ObjectUtils.isEmpty(sheetData) || ObjectUtils.isEmpty(rowDataList)){
            return null;
        }
        BulkOperations ops = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,document);
        int rowSize = rowDataList.size();
        List<Map<String, Object>> resultMaps = new ArrayList<>();
        for (int i = 0; i < rowSize; i++) {
            LinkedHashMap<String, Object> values = rowDataList.get(i).getValues();
            values.putAll(businessMap);
            resultMaps.add(values);
            ops.insert(values);
            /*// 至少要有工步名称数据，否则就不插入mongoDB
            if (values.get("stepName") != null) {
                if(businessMap!=null && !businessMap.isEmpty()){
                    values.putAll(businessMap);
                }
                resultMaps.add(values);
                ops.insert(values);
            }*/
        }
        ops.execute();

        return resultMaps;
    }
}
